package cn.lutu.adapter.controller;

import cn.lutu.domain.service.InvoiceService;
import cn.lutu.protocol.dto.InvoiceAddDTO;
import cn.lutu.security.jwtFilter.CurrentPrincipal;
import cn.lutu.security.security.CustomUserDetails;
import cn.lutu.util.response.ResultVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @date 2023-06-03
 * @author luoyinhan
 * */
@Slf4j
@RestController
@RequestMapping("/v1/invoices/")
public class InvoiceController {

    @Autowired
    InvoiceService invoiceService;


    @ApiOperation("新增发票")
    @PostMapping("/insert")
    public ResultVO addNew(@RequestBody InvoiceAddDTO invoiceAddDTO, @AuthenticationPrincipal @ApiIgnore CurrentPrincipal c) {
        log.debug("开始处理新增发票的请求，参数：{}", invoiceAddDTO);
        invoiceService.addInvoice(invoiceAddDTO, c.getId());
        return ResultVO.ok();
    }

    @ApiOperation("管理员审批发票")
    @PostMapping("{id}/{status}/update")
    public ResultVO updateInvoice(@PathVariable Long id,@PathVariable String status) {
        log.debug("开始处理更新发票的请求，参数：{},{}", id,status);
        invoiceService.updateInvoice(id,status);
        return ResultVO.ok();
    }

    @ApiOperation("查看发票")
    @GetMapping("{orderId}/view")
    public ResultVO viewInvoice(@PathVariable Long orderId) {
        log.debug("开始处理查看发票的请求，参数：{}", orderId);
        return ResultVO.ok(invoiceService.selectByOrderId(orderId));
    }

    @ApiOperation("查看用户所有发票")
    @GetMapping("")
    public ResultVO viewUserInvoice(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal c) {
        log.debug("开始处理查看发票的请求，参数：{}",  c.getId());
        return ResultVO.ok(invoiceService.selectByUserId( c.getId()));
    }

    @ApiOperation("查看所有用户发票")
    @GetMapping("list")
    public ResultVO viewAllInvoice() {
        log.debug("开始处理查看所有发票的请求");
        return ResultVO.ok(invoiceService.selectAllInvoices());
    }

    @ApiOperation("搜索用户发票")
    @GetMapping("search")
    public ResultVO searchUserInvoice(@RequestParam String keyword,@AuthenticationPrincipal @ApiIgnore CurrentPrincipal c) {
        log.debug("开始处理搜索发票的请求，参数：{}",  keyword);
        return ResultVO.ok(invoiceService.selectByKeyword(keyword, c.getId()));
    }
}
